<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/10
 * Time: 10:28
 */
namespace Admin\Controller;

class SumController extends RightController
{
    public function index()
    {
        $cur_date = (date("Y-m-d 00:00:00"));
        if($this->isAdmin())
        {
            // 会员总数
            $allMember=M("Member")->count();
            $this->assign("allMember", $allMember == null ? 0 : $allMember);

            // 今日注册会员数
            $todayMember=M("Member")->where( "createTime>'$cur_date'")->count();
            $this->assign("todayMember", $todayMember == null ? 0 : $todayMember);

            // 代理总数
            $allAgen = M("Agen")->count();
            $this->assign("allAgen", $allAgen == null ? 0 : $allAgen);

            // 下属代理总数
            $xiajiAgen = M()->query("select count(1) as agensum, CONCAT( b.name,'代理') as levelname  from agen a
                                    left join agenlevel b on a.`level`=b.level 
                                    GROUP BY a.`level`");
            $this->assign("xiajiAgen", $xiajiAgen == null ? 0 : $xiajiAgen);




            // 今日代理数
            $todayAgen = M("Agen")->where("createTime>'$cur_date'")->count();
            $this->assign("todayAgen", $todayAgen == null ? 0 : $todayAgen);

            // 总提现
            $allWithdrawal = M("Withdrawal")->where("status='confirmed'")->sum('amount');
            $this->assign("allWithdrawal", $allWithdrawal == null ? 0 : $allWithdrawal);
            // 今日提现
            $todayWithdrawal = M("Withdrawal")->where("status='confirmed' and createtime>'$cur_date'")->sum('amount');
            $this->assign("todayWithdrawal", $todayWithdrawal == null ? 0 : $todayWithdrawal);

            // 代理下单状态统计
            $agenOrderStatus = M('agenorder')
                ->group('status')
                ->field("IFNULL(count(*),0) scount,CASE status WHEN 2 THEN '未发货' WHEN 3 THEN '未收货' WHEN 4 THEN '已完成' WHEN 5 THEN '已取消' WHEN 6 THEN '已驳回' ELSE '错误状态' END sname")
                ->select();
            $this->assign("agenOrderStatus", $agenOrderStatus == null ? 0 : $agenOrderStatus);

            //代理审核状态统计
            $agenReviewStatus = M('agenreview')
                ->group('status')
                ->field('IFNULL(count(*),0) scount,CASE status WHEN 1 THEN \'未审核\' WHEN 2 THEN \'代理已审核\' WHEN 3 THEN \'厂家已审核\' WHEN 4 THEN \'已驳回\' ELSE \'错误状态\' END sname')
                ->select();
            $this->assign("agenReviewStatus", $agenReviewStatus == null ? 0 : $agenReviewStatus);

            //代理报单状态统计
            $agenReportStatus = M('onlineorder')
                ->where('orderType = 3')
                ->group('status')
                ->field('IFNULL(count(*),0) scount,CASE status WHEN 1 THEN \'未支付\' WHEN 2 THEN \'未发货\' WHEN 3 THEN \'未收货\' WHEN 4 THEN \'已完成\' WHEN 5 THEN \'已取消\' WHEN 6 THEN \'已驳回\' ELSE \'错误状态\' END sname')
                ->select();
            $this->assign("agenReportStatus", $agenReportStatus == null ? 0 : $agenReportStatus);
        }else{
            $companyid=$this->getAccount()['companyid'];
            // 会员总数
            $allMember=M("Member")
                       ->alias("t")
                       ->join(" LEFT JOIN agen a ON a.id = t.belongAgenId")
                       ->where(array(' a.companyid'=>$companyid))
                       ->count();
            $this->assign("allMember", $allMember == null ? 0 : $allMember);

            // 今日注册会员数
            $todayMember=M("Member")
                ->alias("t")
                ->join(" LEFT JOIN agen a ON a.id = t.belongAgenId")
                ->where( " a.companyid = $companyid and t.createTime>'$cur_date'" )
                ->count();
            $this->assign("todayMember", $todayMember == null ? 0 : $todayMember);

            // 代理总数
            $allAgen = M("Agen")
                ->alias("t")
                ->where( " t.companyid = $companyid " )
                ->count();
            $this->assign("allAgen", $allAgen == null ? 0 : $allAgen);

            // 下属代理总数
            $xiajiAgen = M()->query("select count(1) as agensum, CONCAT( b.name,'代理') as levelname  from agen a
                                    left join agenlevel b on a.`level`=b.level 
                                    where a.companyid=$companyid
                                    GROUP BY a.`level`");
            $this->assign("xiajiAgen", $xiajiAgen == null ? 0 : $xiajiAgen);






            // 今日代理数
            $todayAgen = M("Agen")
                ->alias("t")
                ->where( " t.companyid = $companyid and t.createTime>'$cur_date'" )
                ->count();
            $this->assign("todayAgen", $todayAgen == null ? 0 : $todayAgen);

            // 总提现
            $allWithdrawal = M("Withdrawal")
                ->alias("t")
                ->join(" LEFT JOIN agen a ON a.id = t.agenid")
                ->where("t.status='confirmed' and a.companyid = $companyid")
                ->sum('t.amount');
            $this->assign("allWithdrawal", $allWithdrawal == null ? 0 : $allWithdrawal);
            // 今日提现
            $todayWithdrawal = M("Withdrawal")
                ->alias("t")
                ->join(" LEFT JOIN agen a ON a.id = t.agenid")
                ->where("t.status='confirmed' and a.companyid = $companyid and t.createtime>'$cur_date'")
                ->sum('t.amount');
            $this->assign("todayWithdrawal", $todayWithdrawal == null ? 0 : $todayWithdrawal);

            // 代理下单状态统计
            $agenOrderStatus = M('agenorder')
                ->alias('o')
                ->join('LEFT JOIN agen a ON a.id = o.SourceId')
                ->where("a.companyid = $companyid")
                ->group('o.status')
                ->field("IFNULL(count(*),0) scount,CASE o.status WHEN 2 THEN '未发货' WHEN 3 THEN '未收货' WHEN 4 THEN '已完成' WHEN 5 THEN '已取消' WHEN 6 THEN '已驳回' ELSE '错误状态' END sname")
                ->select();
            $this->assign("agenOrderStatus", $agenOrderStatus == null ? 0 : $agenOrderStatus);

            //代理审核状态统计
            $agenReviewStatus = M('agenreview')
                ->alias('r')
                ->join('LEFT JOIN agen a ON a.id = r.agenId')
                ->where("a.companyid = $companyid")
                ->group('r.status')
                ->field('IFNULL(count(*),0) scount,CASE r.status WHEN 1 THEN \'未审核\' WHEN 2 THEN \'代理已审核\' WHEN 3 THEN \'厂家已审核\' WHEN 4 THEN \'已驳回\' ELSE \'错误状态\' END sname')
                ->select();
            $this->assign("agenReviewStatus", $agenReviewStatus == null ? 0 : $agenReviewStatus);

            //代理报单状态统计
            $agenReportStatus = M('onlineorder')
                ->alias('o')
                ->join('LEFT JOIN agen a ON a.id = o.SourceId')
                ->where("a.companyid = $companyid and o.orderType = 3")
                ->group('o.status')
                ->field('IFNULL(count(*),0) scount,CASE o.status WHEN 1 THEN \'未支付\' WHEN 2 THEN \'未发货\' WHEN 3 THEN \'未收货\' WHEN 4 THEN \'已完成\' WHEN 5 THEN \'已取消\' WHEN 6 THEN \'已驳回\' ELSE \'错误状态\' END sname')
                ->select();
            $this->assign("agenReportStatus", $agenReportStatus == null ? 0 : $agenReportStatus);

        }
        $this->display();
    }
}